package com.shizhanzhe.zxbapp.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.utils.SharedPreferencesUtils;
import com.shizhanzhe.zxbapp.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BindingPhoneActivity extends AppCompatActivity {

    @BindView(R.id.top_back)
    ImageView topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.ll_wbd)
    LinearLayout llWbd;
    @BindView(R.id.rl_ybd)
    RelativeLayout rlYbd;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_vercode)
    EditText etVercode;
    @BindView(R.id.tv_vercode)
    TextView tvVercode;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    private CountDownTimer time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_binding_phone);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.colorAccent)
                .statusBarDarkFont(true, 0.2f)
                .init();
        topTitle.setText("绑定手机号");
        if (!"".equals(UserBean.mobile)) {
            rlYbd.setVisibility(View.VISIBLE);
            tvPhone.setText("已绑定手机:" + UserBean.mobile);
        } else {
            llWbd.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.top_back, R.id.tv_commit, R.id.tv_vercode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_back:
                finish();
                break;
            case R.id.tv_vercode:
                if (!"".equals(etPhone.getText().toString())) {
                    if (Util.isMobile(etPhone.getText().toString())) {
                        reCode(etPhone.getText().toString());
                        time = new CountDownTimer(60000, 1000) {
                            @SuppressLint("SetTextI18n")
                            @Override
                            public void onTick(long millisUntilFinished) {
                                tvVercode.setClickable(false);
                                tvVercode.setText(millisUntilFinished / 1000 + "秒");
                            }

                            @Override
                            public void onFinish() {
                                tvVercode.setText("重新验证");
                                tvVercode.setClickable(true);
                            }
                        };
                        time.start();
                    } else {
                        Toast.makeText(this, "手机号输入有误", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_commit:
                if (!"".equals(etPhone.getText().toString())) {
                    if (!"".equals(etVercode.getText().toString())) {
                        commit(UserBean.uid,etPhone.getText().toString(),etVercode.getText().toString(),Util.androidId(this));
                    } else {
                        Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }

    private void reCode(String phone) {
        String sign= Util.encode(Util.encode(phone+"zhuanxiaobao")+"zhuanxiaobao");
        OkHttpDownloadJsonUtil.downloadJson(this, Path.sendPhoneCode + "?phone=" + phone+ "&sign=" +sign+"&type=0",
                new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {
                        Log.d("jsonddd", json);
                        if (!"1".equals(json) && !"2".equals(json)) {
                            try {
                                JSONObject jsonObject = new JSONObject(json);
                                if (jsonObject.has("info")) {
                                    Toast.makeText(BindingPhoneActivity.this, jsonObject.getString("info"), Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            Toast.makeText(BindingPhoneActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void commit(int uid, final String phone, String vercode, String androidid) {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.phoneBinding + "?phone=" + phone + "&code=" +vercode
                + "&device=" + androidid + "&uid=" + uid,
                new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {
                        Log.d("jsonddd", json);
                        if (!"1".equals(json) && !"2".equals(json)) {
                            try {
                                JSONObject jsonObject = new JSONObject(json);
                                if (jsonObject.has("info")) {
                                    Toast.makeText(BindingPhoneActivity.this, jsonObject.getString("info"), Toast.LENGTH_SHORT).show();
                                }
                                if (jsonObject.has("status") && jsonObject.getInt("status") == 1){
                                    UserBean.mobile = phone;
                                    SharedPreferencesUtils.setParam(BindingPhoneActivity.this, "mobile", UserBean.mobile);
                                    setResult(100);
                                    finish();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
